<?php

/**
 * Implements hook_field_widget_info().
 */
function universities_picker_field_widget_info() {
    return array(
        'universities_picker' => array(
            'label' => 'Universities Picker',
            'field types' => array('text'),
            'behaviors' => array(
                'multiple values' => FIELD_BEHAVIOR_DEFAULT,
                'default value' => FIELD_BEHAVIOR_DEFAULT,
            ),
            'settings' => array(
                'vocab_id' => 0,
                'root_term_id' => 0,
            ),
        ),
    );
}

/**
 * Implements hook_field_widget_settings_form().
 */
function universities_picker_field_widget_settings_form($field, $instance) {
    $widget = $instance['widget'];
    $settings = $widget['settings'];
    $form = array();

    $form['vocab_id'] = array(
        '#type' => 'textfield',
        '#title' => t('Vocabulary id'),
        '#description' => t("Vocabulary id"),
        '#default_value' => $settings['vocab_id'],
    );

    $form['root_term_id'] = array(
        '#type' => 'textfield',
        '#title' => t('Root term id'),
        '#description' => t("Root term id"),
        '#default_value' => $settings['root_term_id'],
    );

    return $form;
}

/**
 * Implements hook_widget_field_form().
 */
function universities_picker_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
    $module_path = drupal_get_path('module', 'universities_picker');
    $settings = $instance['widget']['settings'];

    switch ($instance['widget']['type']) {
        case 'universities_picker':
            $element['#attached']['js'] = array($module_path . '/univers.js', $module_path . '/schoole_selector.js');
            $element['#type'] = 'textfield';
            //$element['#element_validate'] = array('_universities_picker_widget_validate');
            $element['#default_value'] = $items[$delta]['value'];
    }

    return $element;
}

function _universities_picker_widget_validate($element, &$form_state) {
    //dpm($element);
}